﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace NeedForSmash
{
    class Animation
    {
        private Quad m_quad;

        private List<AnimationSprite> m_animationSpriteList;
        public List<AnimationSprite> AnimationSpriteList
        {
            get
            {
                return m_animationSpriteList;
            }
        }

        private AnimationSprite m_currentAnimation;
        private int m_currentIndex;

        private bool m_isRunning;

        private int m_timeElapsed; // Milliseconds

        public Animation(Quad quad)
        {
            m_quad = quad;
            m_animationSpriteList = new List<AnimationSprite>();
        }

        public void Start()
        {
            if (m_animationSpriteList.Count > 0)
            {
                m_isRunning = true;
                m_timeElapsed = 0;
                m_currentIndex = 0;
                m_currentAnimation = m_animationSpriteList[m_currentIndex];
                DisplayCurrentSprite();
            }
        }

        public void Stop()
        {
            m_isRunning = false;
        }

        public void Update(GameTime gameTime)
        {
            if (m_isRunning)
            {
                m_timeElapsed += gameTime.ElapsedGameTime.Milliseconds;
                if (m_timeElapsed > m_currentAnimation.Duration)
                {
                    Console.WriteLine("Changing texture...");
                    m_timeElapsed = 0;
                    ++m_currentIndex;
                    if (m_currentIndex >= m_animationSpriteList.Count)
                        m_currentIndex = 0;
                    m_currentAnimation = m_animationSpriteList[m_currentIndex];
                    DisplayCurrentSprite();
                }
            }
        }

        private void DisplayCurrentSprite()
        {
            m_quad.textureLowerLeft = m_currentAnimation.TextureLowerLeft;
            m_quad.textureLowerRight = m_currentAnimation.TextureLowerRight;
            m_quad.textureUpperLeft = m_currentAnimation.TextureUpperLeft;
            m_quad.textureUpperRight = m_currentAnimation.TextureUpperRight;
            m_quad.Refresh();
        }
    }
}
